<?php
class NewsController extends Zend_Controller_Action
{
	private $_uiVar = array();
	private $_news;
	
	public function init()
	{
		$this->_uiVar["html"]["title"] = STE_Html::WEBSITE_TITLE;
		
		try
		{
			$db = Zend_Registry::get("DB");
			
			$this->_news = new STE_News($db);
		}
		catch (Exception $ex)
		{
			echo $ex->getMessage();
		}
	}
	
	function indexAction()
	{
		$this->_redirect("news/list");
	}
	
	function listAction()
	{
		$adminSession = new Zend_Session_Namespace("STE_ADMIN");
		if ((isset($adminSession->member["IS_LOGIN"])) && ($adminSession->member["IS_LOGIN"] == 1))
		{
			$smarty = Zend_Registry::get("SMARTY");
			$smarty->assign("uiVar", $this->_uiVar);
			$smarty->display("news/list.tpl");
		}
		else
		{
			$adminSession->redirect["NEXT_URL"] = "/news/list";
			header("Location: /member/login");
		}
	}
	
	function addAction()
	{
		$returnData = array();
		
		$adminSession = new Zend_Session_Namespace("STE_ADMIN");
		if ((isset($adminSession->member["IS_LOGIN"])) && ($adminSession->member["IS_LOGIN"] == 1))
		{
			try
			{
				$formData = $_POST;
				$formData = $this->_trimNewNewsData($formData);
				$errorMessage = $this->_checkNewNewsData($formData);
				if (empty($errorMessage))
				{
					$row = $this->_news->addNews($formData["newNewsCategoryID"], $formData["newNewsLanguageID"], $formData["newNewsTitle"], $formData["newNewsIsHighlight"], $formData["newNewsIsHot"], $formData["newNewsPublishDate"], $formData["newNewsSource"], $formData["newNewsImageURL"], $formData["newNewsSummary"], $formData["newNewsContent"], $adminSession->member["EMAIL"]);
					echo json_encode(array("code" => 0, "data" => "- Thành công. <br />"));
				}
				else
					echo json_encode(array("code" => -99, "data" => $errorMessage));
			}
			catch (Exception $ex)
			{
				echo json_encode(array("code" => -101, "data" => $ex->getMessage()));
			}
		}
		else
			echo json_encode(array("code" => -100, "data" => "- Bạn cần phải đăng nhập để thực hiện chức năng này. <br />"));
	}
	
	function editAction()
	{
		$returnData = array();
		
		$adminSession = new Zend_Session_Namespace("STE_ADMIN");
		if ((isset($adminSession->member["IS_LOGIN"])) && ($adminSession->member["IS_LOGIN"] == 1))
		{
			try
			{
				$formData = $_POST;
				$formData = $this->_trimEditNewsData($formData);
				$errorMessage = $this->_checkEditNewsData($formData);
				if (empty($errorMessage))
				{
					$row = $this->_news->editNews($formData["editNewsID"], $formData["editNewsCategoryID"], $formData["editNewsLanguageID"], $formData["editNewsTitle"], $formData["editNewsIsHighlight"], $formData["editNewsIsHot"], $formData["editNewsPublishDate"], $formData["editNewsSource"], $formData["editNewsImageURL"], $formData["editNewsSummary"], $formData["editNewsContent"], $adminSession->member["EMAIL"]);
					echo json_encode(array("code" => 0, "data" => "- Thành công. <br />"));
				}
				else
					echo json_encode(array("code" => -99, "data" => $errorMessage));
			}
			catch (Exception $ex)
			{
				echo json_encode(array("code" => -101, "data" => $ex->getMessage()));
			}
		}
	}
	
	function removeAction()
	{
		$returnData = array();
		
		$fshopAdminSession = new Zend_Session_Namespace("FSHOP_ADMIN");
		if ((isset($fshopAdminSession->member["IS_LOGIN"])) && ($fshopAdminSession->member["IS_LOGIN"] == 1))
		{
			try
			{
				$formData = $_POST;
				$formData = $this->_trimRemoveCategoryData($formData);
				$errorMessage = $this->_checkRemoveCategoryData($formData);
				if (empty($errorMessage))
				{
					$row = $this->_category->removeCategory($formData["removeCategoryID"]);
					if ($row > 0)
						echo json_encode(array("code" => 0, "data" => "- Thành công. <br />"));
					else
						echo json_encode(array("code" => -98, "data" => "- Có lỗi khi lưu dữ liệu vào cơ sở dữ liệu. <br />"));
				}
				else
					echo json_encode(array("code" => -99, "data" => $errorMessage));
			}
			catch (Exception $ex)
			{
				echo json_encode(array("code" => -101, "data" => $ex->getMessage()));
			}
		}
		else
			echo json_encode(array("code" => -100, "data" => "- Bạn cần phải đăng nhập để thực hiện chức năng này. <br />"));
	}
	
	private function _trimNewNewsData($formData)
	{
		$formData["newNewsLanguageID"] = (int)trim($formData["newNewsLanguageID"]);
		$formData["newNewsCategoryID"] = (int)trim($formData["newNewsCategoryID"]);
		$formData["newNewsTitle"] = trim($formData["newNewsTitle"]);
		if (empty($formData["newNewsIsHighlight"]))
			$formData["newNewsIsHighlight"] = 0;
		else
			$formData["newNewsIsHighlight"] = (int)$formData["newNewsIsHighlight"];
		if (empty($formData["newNewsIsHot"]))
			$formData["newNewsIsHot"] = 0;
		else
			$formData["newNewsIsHot"] = (int)trim($formData["newNewsIsHot"]);
		$formData["newNewsPublishDate"] = trim($formData["newNewsPublishDate"]);
		if (empty($formData["newNewsPublishDate"]))
			$formData["newNewsPublishDate"] = date("Y-m-d");
		else
			$formData["newNewsPublishDate"] = DateTime::createFromFormat("d/m/Y", trim($formData["newNewsPublishDate"]))->format("Y-m-d");
		$formData["newNewsSource"] = trim($formData["newNewsSource"]);
		$formData["newNewsImageURL"] = trim($formData["newNewsImageURL"]);
		$formData["newNewsSummary"] = trim($formData["newNewsSummary"]);
		$formData["newNewsContent"] = trim($formData["newNewsContent"]);
		return $formData;
	}
	
	private function _trimEditNewsData($formData)
	{
		$formData["editNewsID"] = (int)trim($formData["editNewsID"]);
		$formData["editNewsLanguageID"] = (int)trim($formData["editNewsLanguageID"]);
		$formData["editNewsCategoryID"] = (int)trim($formData["editNewsCategoryID"]);
		$formData["editNewsTitle"] = trim($formData["editNewsTitle"]);
		if (empty($formData["editNewsIsHighlight"]))
			$formData["editNewsIsHighlight"] = 0;
		else
			$formData["editNewsIsHighlight"] = (int)$formData["editNewsIsHighlight"];
		if (empty($formData["editNewsIsHot"]))
			$formData["editNewsIsHot"] = 0;
		else
			$formData["editNewsIsHot"] = (int)trim($formData["editNewsIsHot"]);
		$formData["editNewsPublishDate"] = trim($formData["editNewsPublishDate"]);
		if (empty($formData["editNewsPublishDate"]))
			$formData["editNewsPublishDate"] = date("Y-m-d");
		else
			$formData["editNewsPublishDate"] = DateTime::createFromFormat("d/m/Y", trim($formData["editNewsPublishDate"]))->format("Y-m-d");
		$formData["editNewsSource"] = trim($formData["editNewsSource"]);
		$formData["editNewsImageURL"] = trim($formData["editNewsImageURL"]);
		$formData["editNewsSummary"] = trim($formData["editNewsSummary"]);
		$formData["editNewsContent"] = trim($formData["editNewsContent"]);
		return $formData;
	}
	
	private function _trimRemoveCategoryData($formData)
	{
		$formData["removeCategoryID"] = trim($formData["removeCategoryID"]);
		return $formData;
	}
	
	private function _checkNewNewsData($formData)
	{
		$errorMessage = "";
		
		if (empty($formData["newNewsLanguageID"]) || empty($formData["newNewsTitle"]) || ($formData["newNewsCategoryID"] == 0))
			$errorMessage = "- Vui lòng nhập vào các thông tin bắt buộc. <br />";
		
		return $errorMessage;
	}
	
	private function _checkEditNewsData($formData)
	{
		$errorMessage = "";
		
		if (empty($formData["editNewsLanguageID"]) || empty($formData["editNewsTitle"]) || ($formData["editNewsCategoryID"] == 0))
			$errorMessage = "- Vui lòng nhập vào các thông tin bắt buộc. <br />";
		
		return $errorMessage;
	}
	
	private function _checkRemoveCategoryData($formData)
	{
		$errorMessage = "";
		
		if (empty($formData["removeCategoryID"]))
			$errorMessage = "- Vui lòng nhập vào các thông tin bắt buộc. <br />";
		
		return $errorMessage;
	}
}
?>